package net.itbaiyang.pattern.email;

import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import net.itbaiyang.pattern.controller.request.EmailRequest;
import net.itbaiyang.pattern.email.bridge.bridgeMajor.CommonSender;
import net.itbaiyang.pattern.email.bridge.bridgeMajor.DefaultMailSender;
import net.itbaiyang.pattern.model.EmailDetails;
import net.itbaiyang.pattern.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;

//默认邮件服务实现
@Service
@Slf4j
public class DefaultEmailService implements EmailService{

     @Autowired
     private JavaMailSender mailSender; //springboot提供的邮件发送的api

    @Value("${spring.mail.from}")
     private String from;//获取发件人的邮箱地址


    @Override
    public JsonData sendSimplesMail(EmailRequest request) {
        log.info("from===="+from);

        try {
            EmailDetails details = new EmailDetails();
            getMailInfo(request,details);
            CommonSender commonSender = new CommonSender(details);
            DefaultMailSender sender = new DefaultMailSender();
            sender.send(commonSender.getMailInfo(),mailSender);
            log.info("邮件已经发送。");
            return JsonData.buildCodeAndMsg(1,"邮件发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送邮件时发生生异常!", e);
            return JsonData.buildCodeAndMsg(-1,"邮件发送失败");

        }
    }


    /**
     * 将EmailRequest的属性复制到EmailDetails中
     *
     * @param request EmailRequest对象，源数据
     * @param details EmailDetails对象，目标对象
     */
    public void getMailInfo(EmailRequest request, EmailDetails details){
         if (request != null && details != null) {
                BeanUtils.copyProperties(request, details);
                details.setMailSender(mailSender);
                details.setFrom(from);
            }
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            // true 表示需要创建一一个 multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true); //邮件内容包含html
            mailSender.send(message);
            log.info("html邮件发送成功");
        } catch (Exception e) {
            log.error("发送html邮件时发生生异常!", e);
        }
    }
     @Override
     public void sendSimpleMail(String to, String subject, String content) {
         log.info("from===="+from);
         SimpleMailMessage message = new SimpleMailMessage();
         message.setFrom(from); //发件人
         message.setTo(to); //收件人
         message.setSubject(subject); //邮件主题
         message.setText(content); //邮件内容，只能纯文本
         try {
             mailSender.send(message);
             log.info("简单邮件已经发送。");
         } catch (Exception e) {
             e.printStackTrace();
             log.error("发送简单邮件时发生生异常!", e);
         }
     }

     @Override
     public void sendAttachmentsMail (
             String to, String subject,
             String content, String filePath) {
         MimeMessage message = mailSender.createMimeMessage();
         try {
             MimeMessageHelper helper = new MimeMessageHelper(message, true);
             helper.setFrom(from);
             helper.setTo(to);
             helper.setSubject(subject);
             helper.setText(content, true);
             FileSystemResource file = new FileSystemResource(new File(filePath));
             String fileName = file.getFilename();
             helper.addAttachment(fileName, file); //设置附件

             // helper.addAttachment("test"+fileName, file);
             mailSender.send(message);
             log.info("带附件的邮件已经发送。");
         } catch (Exception e) {
             log.error("发送带附件的邮件时发生生异常!", e);
         }
     }
     @Override
     public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
         MimeMessage message = mailSender.createMimeMessage();
         try {
             MimeMessageHelper helper = new MimeMessageHelper(message, true);
             helper.setFrom(from);
             helper.setTo(to);
             helper.setSubject(subject);
             helper.setText(content, true);
             FileSystemResource res = new FileSystemResource(new File(rscPath));
             helper.addInline(rscId, res); //内嵌静态资源
             mailSender.send(message);
             log.info("嵌入静态资源的邮件已经发送。");
         } catch (Exception e) {
             log.error("发送嵌入静态资源的邮件时发生异常!", e);
         }
     }


}
